'use strict';
var redis = require('redis');
var redisClient;
var redisAvailable = false;
var STR_KEY_EXPIRE = require(process.argv[2]).STR_KEY_EXPIRE;
var HASH_KEY_EXPIRE = require(process.argv[2]).HASH_KEY_EXPIRE;


var log4js = require('log4js');
var log = log4js.getLogger("redis");


exports.init = function (config) {
    if (!!redisClient) {
      return redisClient;
    }
    redisClient = redis.createClient({
        url: config.URL
    });
    redisClient.on('error', function (error) {
      log.warn('connect error===>' + error);
      redisAvailable = false;
    });
    redisClient.on('end', function () {
      log.debug('connect end===>');
      redisAvailable = false;
    });
    redisClient.on('connect', function () {
      log.debug('connect redis===>');
      redisAvailable = true;
    });

};

exports.getHashCache = function (key, field, isStoreJsonField, cb) {
  if (!redisAvailable) {
    return cb(new Error('redis connect error'));
  }
  if (typeof field === 'function') {
    cb = field;
    field = false;
    isStoreJsonField = false;
  }
  if (typeof isStoreJsonField === 'function') {
    isStoreJsonField = false;
    cb = isStoreJsonField;
  }
  //  获取单条HASH 字段
  if (field) {
    redisClient.hget(key, field, function (err, obj) {
      if (err) {
        return cb(err);
      }
      if (obj) {
        if (isStoreJsonField) {
          var data = {};
          data = JSON.parse(obj.toString());
          cb(err, data);
        } else {
          cb(err, obj);
        }
      }
    });
    return;
  }
  // 获得整个hash
  redisClient.hgetall(key, function (err, obj) {
    if (err) {
      return cb(err);
    }
    var data = {};
    if (obj) {
      if (isStoreJsonField) {
        data = obj;
      } else {
        for (var userId in obj) {
          data[userId] = JSON.parse(obj[userId]);
        }
      }
    }
    cb(err, data);
  });
};

exports.setHashCache = function (key, field, value, expire, cb) {
  if (!redisAvailable) {
    return cb(new Error('redis connect error'));
  }

  if (field instanceof Array) {
    if (typeof value === 'function') {
      cb = value;
      expire = HASH_KEY_EXPIRE;
    }
    if (typeof expire === 'function') {
      cb = expire;
      expire = value;
    }
    var fields = field;
    fields.forEach(function (field, index) {
       if (field instanceof Object) {
         fields[index] = JSON.stringify(field);
       }
    });
    redisClient.hmset(key, fields, function (err, result) {
      if (err) {
        return cb(err);
      }
       //设置过期时间
      redisClient.expire(key, expire);
      return cb(null, result);
    });
  } else {
    if (typeof expire === 'function') {
      cb = expire;
      expire = HASH_KEY_EXPIRE;
    }
    value = JSON.stringify(value);
    redisClient.hset(key, field, value, function (err, result) {
      if (err) {
        return cb(err);
      }
       //设置过期时间
      redisClient.expire(key, expire);
      return cb(null, result);
    });
  }
};

exports.getStrCache = function (key, cb) {
  if (!redisAvailable) {
    return cb(new Error('redis connect error'));
  }
  redisClient.get(key, function(err, result) {
    if (err) {
      return cb(err);
    }
    var data = null;
    if (result) {
      data = JSON.parse(result.toString());
    }
    return cb(null, data);
  });
};

exports.setStrCache = function (key, value, expire, cb) {
  if (!redisAvailable) {
    return cb(new Error('redis connect error'));
  }
  if (!value) {
   return cb(new Error('value is undefined or null'));
 }
 // default expire time
 if (!expire) {
   expire = STR_KEY_EXPIRE;
 }
 value = JSON.stringify(value);
 redisClient.setex(key, expire, value, function(err, result) {
    if (err) {
      return cb(err);
    }
    return cb(null, result);
  });
};
